package cj.web.marry.controllers;

import javax.servlet.http.HttpServletResponse;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.web.BaseController;
import cj.web.mall.dao.UserDAO;
import cj.web.marry.constants.MemberComTypeEnum;
import cj.web.marry.dao.AppTokenDAO;

@Path("xqLoginCmp")
public class XQLoginCmpController extends BaseController{
	@Autowired
	private UserDAO userDao;
	@Autowired
	private AppTokenDAO appTokenDao;
	
	@Get("login")
	public String login(Model model, @Param("returnPage")String returnPage){
		if(returnPage != null && returnPage.length() > 0){
			model.add("returnPage", returnPage);
		}
		return "/views/marry/meet/login.vm";
	}
	
	@Post("register")
	public synchronized String register(@Param("phone")String phone, @Param("token")String token, 
			@Param("returnPage")String returnPage, HttpServletResponse response){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		if( appTokenDao.queryToken(phone, token) > 0 ){
			int buyerId = userDao.getBuyerId(phone);
			int isExist = userDao.isExistMemberDetailCmp(buyerId,MemberComTypeEnum.NUM_1.getValue());
			
			if(isExist==0){
				jo.put("returnPage", "xqLoginCmp/fail");
			}else{
				jo.put("returnPage", returnPage);
				setCookie(response, "phone", phone);
			}
		}
		else{
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	@Get("fail")
	public String fail(Model model){
		return "/views/marry/meet/cmpActivity/fail.vm";
	}
}